package com.ptswitch.material.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxzon.project.EHibernateDaoSupport;
import com.hxzon.util.StringUtil;
import com.ptswitch.material.dao.UserDao;
import com.ptswitch.material.model.Uzer;

@Repository("userDao")
public class UserHibernate extends EHibernateDaoSupport<Uzer> implements UserDao {

	public Uzer getByName(String username) {
		if (username == null || username.isEmpty()) {
			return null;
		}
		username = StringUtil.escapeSql(username);
		List<Uzer> list = getList(" where p.username='" + username + "'", -1, -1);
		if (list.isEmpty()) {
			return null;
		}
		if (list.size() > 1) {
			logger.error("user[{}] not unique", username);
		}
		return list.get(0);
	}
	
	public Uzer getByNameAndPassword(String username,String password){
		if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
			return null;
		}
		username=StringUtil.escapeSql(username);
		password=StringUtil.escapeSql(password);
		List<Uzer> list =getList(" where p.username='"+username+"' and p.password='"+password+"'");
		if (list.isEmpty()) {
			return null;
		}
		if (list.size() > 1) {
			logger.error("user[{}] not unique", username);
		}
		return list.get(0);
	}
	
	public String orderBy(){
		return " order by p.username ";
	}

}
